package com.gxa.customer.security.security.handler;

import com.alibaba.fastjson.JSONObject;
import com.gxa.common.commons.R;
import org.springframework.http.MediaType;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 未授权统一处理
 */
public class UnauthEntryPoint implements AuthenticationEntryPoint {
    //未授权统一返回
    @Override
    public void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
        R r = new R();
        r.setCode(500);
        r.setMsg("没有登陆注册进行授权");
        httpServletResponse.setHeader("Content-type", "text/html;charset=UTF-8");
        httpServletResponse.setContentType(MediaType.APPLICATION_JSON_VALUE);
        httpServletResponse.getWriter().write(JSONObject.toJSONString(r));

    }
}
